package com.suning.sawp.service.impl.returngoods;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.bi.StoreRetGdsRankListDto;
import com.suning.sawp.dto.returngoods.RetGdsRateChartRtn;
import com.suning.sawp.dto.returngoods.RetGdsStatisListRtn;
import com.suning.sawp.intf.returngoods.ReturnGoodsDaoService;
import com.suning.sawp.service.util.DateUtils;

@Service
public class ReturnGoodsDaoServiceImpl implements ReturnGoodsDaoService {

    @Resource
    DalClient                   dalClient;

    private static final String DATE_FORMAT = "yyyyMMdd";
    
    private static final String MONTH_FORMAT = "yyyyMM";

    @Override
    public List<RetGdsRateChartRtn> queryRetGdsStatisByDay(String storeCode, String categoryCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DATE, -1);
        //取昨天的日期作为查询截止日期
        String endTime = DateUtils.getDateStrByFormat(c1.getTime(), DATE_FORMAT);
        //取昨天的日期往前推13天作为查询开始日期
        c1.add(Calendar.DATE, -13);
        String startTime = DateUtils.getDateStrByFormat(c1.getTime(), DATE_FORMAT);
        paramMap.put("storeCode", storeCode);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        if (StringUtils.isNotBlank(categoryCode)) {
            paramMap.put("categoryCode", categoryCode);
            return dalClient.queryForList("BI_STORE_RET_GDS.QUERY_STORE_CATE_STATIS_BY_DAY", paramMap,
                    RetGdsRateChartRtn.class);
        }
        return dalClient.queryForList("BI_STORE_RET_GDS.QUERY_STORE_STATIS_BY_DAY", paramMap,
                RetGdsRateChartRtn.class);
    }

    @Override
    public List<RetGdsRateChartRtn> queryRetGdsStatisByMonth(String storeCode, String categoryCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        Calendar c1 = Calendar.getInstance();
        //取昨天日期所在的月份为查询截止月份
        c1.add(Calendar.DATE, -1);
        paramMap.put("nowMonth",DateUtils.getDateStrByFormat(c1.getTime(), MONTH_FORMAT));
        //取昨天日期所在的月份往前推5个月作为查询开始月份
        c1.add(Calendar.MONTH, -5);
        paramMap.put("preMonth", DateUtils.getDateStrByFormat(c1.getTime(), MONTH_FORMAT));
        paramMap.put("storeCode", storeCode);
        if (StringUtils.isNotBlank(categoryCode)) {
            paramMap.put("categoryCode", categoryCode);
            return dalClient.queryForList("BI_STORE_RET_GDS.QUERY_STORE_CATE_STATIS_BY_MONTH", paramMap,
                    RetGdsRateChartRtn.class);
        }
        return dalClient.queryForList("BI_STORE_RET_GDS.QUERY_STORE_STATIS_BY_MONTH", paramMap,
                RetGdsRateChartRtn.class);
    }

    @Override
    public List<RetGdsRateChartRtn> queryRetGdsStatisByWeek(String storeCode, String categoryCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DATE, -1);
        //获取昨天的日期
        paramMap.put("nowTime", DateUtils.getDateStrByFormat(c1.getTime(), DATE_FORMAT));
        if(StringUtils.isNotBlank(categoryCode)){
            paramMap.put("categoryCode", categoryCode);
            return dalClient.queryForList("BI_STORE_RET_GDS.QUERY_STORE_CATE_STATIS_BY_WEEK", paramMap,
                    RetGdsRateChartRtn.class);
        }
        return dalClient.queryForList("BI_STORE_RET_GDS.QUERY_STORE_STATIS_BY_WEEK", paramMap,
                RetGdsRateChartRtn.class);
    }

    @Override
    public List<RetGdsStatisListRtn> queryRetGdsStatisDetailByDay(List<String> brachCodes,
            List<String> categoryCodes, String sortType, String sumType, Integer startIndex,
            Integer limit, String storeCode) {
        Map<String, Object> paramMap = createQueryMap(brachCodes, categoryCodes, sortType, sumType,
                startIndex, limit, storeCode);
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DATE, -1);
        paramMap.put("date", DateUtils.getDateStrByFormat(c1.getTime(), DATE_FORMAT));
        List<RetGdsStatisListRtn> list = dalClient.queryForList(
                "BI_STORE_RET_GDS.QUERY_STORE_STATIS_DETAIL_BY_DAY", paramMap,
                RetGdsStatisListRtn.class);
        return list;
    }

    private Map<String, Object> createQueryMap(List<String> brachCodes, List<String> categoryCodes,
            String sortType, String sumType, Integer startIndex, Integer limit, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        if (startIndex != null && limit != null) {
            paramMap.put("startIndex", startIndex);
            paramMap.put("maxCount", limit);
        }
        paramMap.put("sortType", sortType);
        if (CollectionUtils.isNotEmpty(brachCodes)) {
            paramMap.put("brachCodes", brachCodes);
        } else if (CollectionUtils.isNotEmpty(categoryCodes)) {
            paramMap.put("categoryCodes", categoryCodes);
        }
        if (CollectionUtils.isEmpty(brachCodes)
                && RetGdsStatisListRtn.sumType.SUM_Y.equals(sumType)) {
            paramMap.put("sumType", 1);
        }
        return paramMap;
    }

    @Override
    public List<RetGdsStatisListRtn> queryRetGdsStatisDetailByMonth(List<String> brachCodes,
            List<String> categoryCodes, String sortType, String sumType, Integer startIndex,
            Integer limit, String storeCode) {
        Map<String, Object> paramMap = createQueryMap(brachCodes, categoryCodes, sortType, sumType,
                startIndex, limit, storeCode);
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DATE, -1);
        int month = c1.get(Calendar.MONTH) + 1;
        int year = c1.get(Calendar.YEAR);
        if(paramMap.get("sumType") != null){
            paramMap.put("month", DateUtils.getDateStrByFormat(c1.getTime(),MONTH_FORMAT));
        }else{
            paramMap.put("month", month);
            paramMap.put("year", year);
        }
        List<RetGdsStatisListRtn> list = dalClient.queryForList(
                "BI_STORE_RET_GDS.QUERY_STORE_STATIS_DETAIL_BY_MONTH", paramMap,
                RetGdsStatisListRtn.class);
        return list;
    }

    @Override
    public List<RetGdsStatisListRtn> queryRetGdsStatisDetailByWeek(List<String> brachCodes,
            List<String> categoryCodes, String sortType, String sumType, Integer startIndex,
            Integer limit, String storeCode) {
        Map<String, Object> paramMap = createQueryMap(brachCodes, categoryCodes, sortType, sumType,
                startIndex, limit, storeCode);
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DATE, -1);
        paramMap.put("nowTime", DateUtils.getDateStrByFormat(c1.getTime(), DATE_FORMAT));
        List<RetGdsStatisListRtn> list = dalClient.queryForList(
                "BI_STORE_RET_GDS.QUERY_STORE_STATIS_DETAIL_BY_WEEK", paramMap,
                RetGdsStatisListRtn.class);
        return list;
    }

    @Override
    public List<StoreRetGdsRankListDto> queryStoreRetGdsRankList(String distCode, String month) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("distCode", distCode);
        paramMap.put("month", month);
        List<StoreRetGdsRankListDto> rankList = dalClient.queryForList(
                "BI_STORE_RET_GDS_MONTH.QUERY_STORE_RANK_LIST", paramMap,
                StoreRetGdsRankListDto.class);
        return rankList;
    }

    @Override
    public List<StoreRetGdsRankListDto> queryStoreCateRetRankList(String distCode, 
            String cateCode, String month) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("distCode", distCode);
        paramMap.put("cateCode", cateCode);
        paramMap.put("month", month);
        List<StoreRetGdsRankListDto> rankList = dalClient.queryForList(
                "BI_STORE_CATE_RET_GDS_MONTH.QUERY_STORE_CATE_RANK_LIST", paramMap,
                StoreRetGdsRankListDto.class);
        return rankList;
    }

    @Override
    public int queryCountRetGdsDetailByDay(List<String> brachCodes, List<String> categoryCodes,
            String sortType, String sumType, String storeCode) {
        Map<String, Object> paramMap = createQueryMap(brachCodes, categoryCodes, sortType, sumType,
                null, null, storeCode);
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DATE, -1);
        paramMap.put("date", DateUtils.getDateStrByFormat(c1.getTime(), DATE_FORMAT));
        Map<String, Object> numMap = dalClient.queryForMap(
                "BI_STORE_RET_GDS.QUERY_COUNT_STORE_DETAIL_BY_DAY", paramMap);
        return numMap != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public int queryCountRetGdsDetailByMonth(List<String> brachCodes, List<String> categoryCodes,
            String sortType, String sumType, String storeCode) {
        Map<String, Object> paramMap = createQueryMap(brachCodes, categoryCodes, sortType, sumType,
                null, null, storeCode);
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DATE, -1);
        int month = c1.get(Calendar.MONTH) + 1;
        int year = c1.get(Calendar.YEAR);
        if(paramMap.get("sumType") != null){
            paramMap.put("month", DateUtils.getDateStrByFormat(c1.getTime(),MONTH_FORMAT));
        }else{
            paramMap.put("month", month);
            paramMap.put("year", year);
        }
        Map<String, Object> numMap = dalClient.queryForMap(
                "BI_STORE_RET_GDS.QUERY_COUNT_STORE_DETAIL_BY_MONTH", paramMap);
        return numMap != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public int queryCountRetGdsDetailByWeek(List<String> brachCodes, List<String> categoryCodes,
            String sortType, String sumType, String storeCode) {
        Map<String, Object> paramMap = createQueryMap(brachCodes, categoryCodes, sortType, sumType,
                null, null, storeCode);
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DATE, -1);
        paramMap.put("nowTime", DateUtils.getDateStrByFormat(c1.getTime(), DATE_FORMAT));
        Map<String, Object> numMap = dalClient.queryForMap(
                "BI_STORE_RET_GDS.QUERY_COUNT_STORE_DETAIL_BY_WEEK", paramMap);
        return numMap != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

}
